// pages/list/list.js
import {baseApi} from '../../utils/util.js';
Page({
  data: {
    // 页面数据
    openid:'',
    code:''
  },
  onLoad() {
    var that = this;
    wx.getStorage({
      key: 'openid',
      success (res) {
        console.log(JSON.stringify(res));
        that.data.openid = res.data;
        console.log(res.data);
      }
    })
  },
  check(){
    var that = this;
    if(!this.data.code){
      wx.showToast({
        title: '请输入访客码',
      });
      return;
    }
    wx.request({
      url:  baseApi() + '/app/checkVisitorCode',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 关键请求头
      },
      data: {
        openid: that.data.openid,
        code: that.data.code
      },
      success(res){
        console.log(JSON.stringify(res));
        wx.showModal({
          title: '提示',
          content: res.data.msg,
          success(res) {
            if (res.confirm) {
              // 再次调用授权方法
            }
          }
        });
      }
    })
  },
  scan(){
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        console.log(res)
        that.setData({
          code:res.result
        })
        that.check();
      }
    })
  }
})